package com.sinai.mshab.client.pages;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.client.ui.aggregate.FormData;
import com.sinai.mshab.client.ui.aggregate.Page;
import com.sinai.mshab.client.ui.aggregate.PageContainer;
import com.sinai.mshab.client.ui.basic.BasicFormComponent;
import com.sinai.mshab.client.ui.basic.PasswordComponent;
import com.sinai.mshab.client.ui.basic.TextQuestionComponent;
import com.sinai.mshab.client.ui.basic.Warning;
import com.sinai.mshab.client.ui.validate.EmailValidator;
import com.sinai.mshab.client.ui.validate.NonEmptyValidator;

/**
 * This page allows the user to register an account with the system.
 * @author serge
 *
 */
public class Register extends Page {
	
	/**
	 * The text fields that we'll need.
	 */
	BasicFormComponent lastname, firstname, email, username, password, password2;
	
	public Register(PageContainer page_container) {
		super(page_container);
	}

	public void initialize() {
		
		container.getEastPanel().setVisible(false);
		container.getWestPanel().setVisible(false);
		
		FormData.getKey().getKeys().clear();
		
		// adding a title
		Label title = new Label("New User Registration");
		title.setStyleName("h3");
		
		// The explanation of survey
		String greet = "Please provide the following information. Once your account" +
				" has been created, you will need to have it activated by contacting" +
				" the application maintainers.";
		Label greeting = new Label(greet);
		greeting.setStyleName("explanation");
		
		// adding the autopopulate fields.
		firstname = new TextQuestionComponent("first_name", "users", "Last Name:", "", "Please specify your first name");
		lastname = new TextQuestionComponent("last_name", "users", "First Name:", "", "Please specify your last name");
		email = new TextQuestionComponent("email", "users", "Email:","", "Please specify your email");
		username = new TextQuestionComponent("username", "users", "Username:", "", "Please specify your username");
		password = new PasswordComponent("password", "users", "Password:", "", "Please type your password");
		password2 = new PasswordComponent("password", "users", "Retype Password:", "", "Please retype your password");
		
		lastname.setValidator(new NonEmptyValidator());
		firstname.setValidator(new NonEmptyValidator());
		email.setValidator(new EmailValidator());
		username.setValidator(new NonEmptyValidator());
		password.setValidator(new NonEmptyValidator());
		password2.setValidator(new NonEmptyValidator());
		
		// the register button
		Button register_button = new Button("Register");
		register_button.addClickListener(new RegisterListener());
		register_button.setStyleName("register_button");
		
		// adding all components
		this.add(title);
		this.add(greeting);
		this.add(lastname);
		this.add(firstname);
		this.add(email);
		this.add(username);
		this.add(password);
		this.add(password2);
		this.add(register_button);
	}
	
	public boolean validate() {
		boolean success = true;
		success = success && lastname.validate();
		success = success && firstname.validate();
		success = success && email.validate();
		success = success && username.validate();
		success = success && password.validate();
		success = success && password2.validate();
		
		boolean match = password2.getAnswer().getFirstAnswer().equals(password.getAnswer().getFirstAnswer());
		
		if (!match) {
			new Warning("The two passwords entered do not match.", 300, 100);
		}
		
		success = success && match;
		return success;
	}
	
	/**
	 * This listener is attached to the button that registers new users.
	 */
	private class RegisterListener implements ClickListener {

		public void onClick(Widget sender) {
			// make sure all info is entered
			validate();			
			
			// reset any info FormData has
			FormData.getInstance().reset();
			
			// send all info to page data
			submit();
			
			// tell form data to send off information
			FormData.getInstance().submitData();
			
			container.loadPage("Login");
		}
	}
}
